#pragma once

#include "common_data.h"
#include "broker.h"

#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <netinet/tcp.h>
#include <assert.h>
#include <errno.h>
#include <arpa/inet.h>
#include <ev.h>

#define CTL_SERVER_PORT 48700
#define CTL_SERVER_IP INADDR_ANY
#define CTL_TIMER 10.0
#define CTL_TIME_KEEPALIVE 10000

#ifdef __cplusplus
extern "C" {
#endif

typedef enum{
    CTL_TASK,
    CTL_INTERRUPT
} CTL_COMMAND_t;

int init_ctl_srv(int host, int port);

void run_ctl_srv();

void enable_keepalive(int sock);

int recv_cmd(int sock, CTL_COMMAND_t* cmd);

int send_cmd(int sock, CTL_COMMAND_t* cmd);

int recv_task(int sock, flight_task_t* tsk);

int send_task(int sock, flight_task_t* tsk);

static void timeout_cb(struct ev_loop *loop, ev_timer *w, int revents);

static void client_read_cb(struct ev_loop *loop, ev_io *w, int revents);

static void on_accept_cb(struct ev_loop *loop, ev_io *w, int revents);

#ifdef __cplusplus
};
#endif
